﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
    public static class BlockStateByte_24_26
    {
        #region constants

        private const byte LowVoltageValue = 0x01;
        private const byte BatteryErrorValue = 0x02;
        private const byte NetworkErrorValue = 0x04;
        private const byte DoorContactValue = 0x08;

        private const byte GeneralDeviceDisruptionValue = 0x01;

        private const byte DisruptionTransmissionPath1Value = 0x01;
        private const byte DisruptionTransmissionPath2Value = 0x02;

        #endregion constants

        public static byte CreateOctet01(bool lowVoltage, bool batteryError, bool networkError, bool doorContact)
        {
            // following the examples in the spec we set all unused bits to 1
            byte octet = 0xf0;

            if (lowVoltage)
            {
                octet += LowVoltageValue;
            }

            if (batteryError)
            {
                octet += BatteryErrorValue;
            }

            if (networkError)
            {
                octet += NetworkErrorValue;
            }

            if (doorContact)
            {
                octet += DoorContactValue;
            }

            return octet;
        }

        public static byte CreateOctet02(bool generalDeviceDisruption)
        {
            // following the examples in the spec we set all unused bits to 1
            byte octet = 0xfe;

            if (generalDeviceDisruption)
            {
                octet += GeneralDeviceDisruptionValue;
            }

            return octet;
        }

        public static byte CreateOctet03(bool disruptionTransmissionPath1, bool disruptionTransmissionPath2)
        {
            // following the examples in the spec we set all unused bits to 1
            byte octet = 0xfc;

            if (disruptionTransmissionPath1)
            {
                octet += DisruptionTransmissionPath1Value;
            }

            if (disruptionTransmissionPath2)
            {
                octet += DisruptionTransmissionPath2Value;
            }

            return octet;
        }

        public static void ParseOctet01(byte octet, out bool lowVoltage, out bool batteryError, out bool networkError, out bool doorContact)
        {
            lowVoltage = ((octet &= LowVoltageValue) > 0);
            batteryError = ((octet &= BatteryErrorValue) > 0);
            networkError = ((octet &= NetworkErrorValue) > 0);
            doorContact = ((octet &= DoorContactValue) > 0);
        }

        public static void ParseOctet02(byte octet, out bool generalDeviceDisruption)
        {
            generalDeviceDisruption = ((octet &= GeneralDeviceDisruptionValue) > 0);           
        }

        public static void ParseOctet03(byte octet, out bool disruptionTransmissionPath1, out bool disruptionTransmissionPath2)
        {
            disruptionTransmissionPath1 = ((octet &= DisruptionTransmissionPath1Value) > 0);
            disruptionTransmissionPath2 = ((octet &= DisruptionTransmissionPath2Value) > 0);
        }
    }
}
